package io.renren.common.utils;



import io.renren.modules.nas.entity.UploadFileEntity;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : lcy
 * @version 1.0
 * @date : 2019/4/17 17:05
 * 文件的相对上传路径转 url前缀路径
 */
public class UploadFilePathToUrlUtils {

    public static String getOssPath() {
        return Constant.conf.getProperty("uploadFile.prefix.ossPath");
    }

    public static String getUrl() {
        return Constant.conf.getProperty("uploadFile.prefix.url");
    }

    /**
     * 将单个文件相对路径转为绝对路径
     *
     * @param filePath
     * @return
     */
    public static String getFilePathToPicUrl(String filePath) {
        String tempUrl=StringUtil.null2Str(filePath);
        if (tempUrl.contains("http")){
            return tempUrl;
        }
        if (StringUtil.isBlank(tempUrl)) {
            return "";
        }
        return getUrl() + StringUtil.null2Str(filePath);
    }

    /**
     * 将pojo中的相对路径转绝对路径
     *
     * @param uploadFileEntity
     * @return
     */
    public UploadFileEntity getWrapperFilePath(UploadFileEntity uploadFileEntity) {
        String picUrl = getUrl() + uploadFileEntity.getFilePath();
        uploadFileEntity.setFilePath(picUrl);
        return uploadFileEntity;
    }

    public List<String> getPicUrlFromStrFilePathOfList(List<String> filePathStrList) {
        return filePathStrList.stream().map(str -> getUrl() + str).collect(Collectors.toList());
    }


    public List<UploadFileEntity> getWrapperFilePathFromList(List<UploadFileEntity> list) {
        list.forEach(uploadFileEntity -> {
            uploadFileEntity.setFilePath(getUrl() + uploadFileEntity.getFilePath());
        });
        return list;
    }
}
